Conditional Branch Frame Barrier

ABSTRACT

Establishing a conditional branch frame barrier is described. A conditional branch in a function epilogue is used to provide frame-specific control. The conditional branch evaluates a return condition to determine whether to return from a callee function to a calling function, or to execute a slow path instead. The return condition is evaluated based on a thread local value. The thread local value is set such that returns to potentially unsafe frames in a call stack are prohibited. The prohibition to return to a potentially unsafe frame may be referred to as a “frame barrier.” Additionally, the thread local value may be used to establish safepointing and/or thread local handshakes, both after execution of a function body and after execution of a loop body.

INCORPORATION BY REFERENCE; DISCLAIMER

Each of the following applications are hereby incorporated by reference:application No. 62/748,369 filed on Oct. 19, 2018. The Applicant herebyrescinds any disclaimer of claim scope in the parent application(s) orthe prosecution history thereof and advises the USPTO that the claims inthis application may be broader than any claim in the parentapplication(s).

TECHNICAL FIELD

The present disclosure relates to call stacks. In particular, thepresent disclosure relates to conditional branch frame barriers.

BACKGROUND

A compiler converts source code, which is written according to aspecification directed to the convenience of the programmer, to eithermachine or object code, which is executable directly by the particularmachine environment, or an intermediate representation (“virtual machinecode/instructions”), such as bytecode, which is executable by a virtualmachine that is capable of running on top of a variety of particularmachine environments. The virtual machine instructions are executable bythe virtual machine in a more direct and efficient manner than thesource code. Converting source code to virtual machine instructionsincludes mapping source code functionality from the language to virtualmachine functionality that utilizes underlying resources, such as datastructures. Often, functionality that is presented in simple terms viasource code by the programmer is converted into more complex steps thatmap more directly to the instruction set supported by the underlyinghardware on which the virtual machine resides.

The approaches described in this section are approaches that could bepursued, but not necessarily approaches that have been previouslyconceived or pursued. Therefore, unless otherwise indicated, it shouldnot be assumed that any of the approaches described in this sectionqualify as prior art merely by virtue of their inclusion in thissection.

BRIEF DESCRIPTION OF THE DRAWINGS

The embodiments are illustrated by way of example and not by way oflimitation in the figures of the accompanying drawings. It should benoted that references to “an” or “one” embodiment in this disclosure arenot necessarily to the same embodiment, and they mean at least one. Inthe drawings:

FIG. 1 illustrates an example computing architecture in which techniquesdescribed herein may be practiced;

FIG. 2 is a block diagram illustrating one embodiment of a computersystem suitable for implementing methods and features described herein;

FIG. 3 illustrates an example virtual machine memory layout in blockdiagram form according to an embodiment;

FIG. 4 illustrates an example frame in block diagram form according toan embodiment;

FIG. 5 illustrates an example garbage collection system for processingobjects and/or items in a heap, in accordance with an embodiment;

FIG. 6 illustrates an example set of stages of execution of a function,in accordance with one or more embodiments;

FIG. 7 illustrates an example set of operations for using a conditionalbranch in a function epilogue to provide frame-specific control, inaccordance with one or more embodiments;

FIGS. 8A-8C illustrates an example set of operations for setting acondition of a conditional branch, in accordance with one or moreembodiments;

FIG. 9 illustrates an example set of operations for performing variouscontrols on execution of a thread, including frame-specific control, inaccordance with one or more embodiments;

FIG. 10 illustrates an example set of operations for handling a framebarrier, in accordance with one or more embodiments;

FIG. 11 illustrates an example set of operations for using a conditionalbranch before a back edge of a loop, in accordance with one or moreembodiments;

FIGS. 12A-12D illustrate an example conditional branch frame barrier, inaccordance with one or more embodiments;

FIG. 13 shows a block diagram that illustrates a computer system, inaccordance with one or more embodiments.

DETAILED DESCRIPTION

In the following description, for the purposes of explanation, numerousspecific details are set forth in order to provide a thoroughunderstanding. One or more embodiments may be practiced without thesespecific details. Features described in one embodiment may be combinedwith features described in a different embodiment. In some examples,well-known structures and devices are described with reference to ablock diagram form in order to avoid unnecessarily obscuring the presentinvention.

-   -   1. GENERAL OVERVIEW    -   2. ARCHITECTURAL OVERVIEW        -   2.1 EXAMPLE CLASS FILE STRUCTURE        -   2.2 EXAMPLE VIRTUAL MACHINE ARCHITECTURE        -   2.3 LOADING, LINKING, AND INITIALIZING    -   3. GARBAGE COLLECTION    -   4. CALL STACKS AND FUNCTION STAGES    -   5. USING A CONDITIONAL BRANCH IN A FUNCTION EPILOGUE TO PROVIDE        FRAME-SPECIFIC CONTROL    -   6. SETTING A CONDITION OF A CONDITIONAL BRANCH    -   7. PERFORMING VARIOUS CONTROLS ON EXECUTION OF A THREAD,        INCLUDING FRAME-SPECIFIC CONTROL    -   8. HANDLING A FRAME BARRIER    -   9. USING A CONDITIONAL BRANCH BEFORE A BACK EDGE OF A LOOP    -   10. EXAMPLE EMBODIMENT    -   11. MISCELLANEOUS; EXTENSIONS    -   12. HARDWARE OVERVIEW

1. General Overview

One or more embodiments include using a conditional branch in a functionepilogue to provide frame-specific control.

A conditional branch is inserted into a function epilogue. If a returncondition is satisfied, then a slow path is executed before returningfrom a callee function to a calling function. If the return condition isnot satisfied, then the callee function returns to the calling functionwithout executing the slow path. The return condition is based on acomparison between a stack pointer and a thread local value. As anexample, a return condition may be that a stack pointer is greater thanor equal to a thread local value.

A thread local value is set such that returns to potentially unsafeframes in a call stack are prohibited. The thread local value is setsuch that a return condition is satisfied when evaluated in a functionepilogue of a callee function that would otherwise return to apotentially unsafe frame. The thread local value is updated such that aprevious prohibition to return to a particular frame is disarmed oncethe particular frame becomes safe, without waiting for all frames of thecall stack to become safe (as is the case with thread local handshakes),or waiting for all call stacks of all threads to become safe (as is thecase with safepointing). The prohibition to return to a potentiallyunsafe frame may be referred to herein as a “watermark frame barrier,”or “frame barrier.” Using a conditional branch that is evaluated basedon a thread local value, a jump to a slow path may be performed withoutchanging any return addresses in a call stack.

Some external processes (external to a program thread associated with acall stack) require snapshots of each frame of the call stack, whereinall snapshots reflect a state of the call stack at a particular time.Examples of such external processes include stack scanning in garbagecollection (GC), a deoptimization process that replacespreviously-optimized native code, stack sampling for inspecting anexecution of a program, and stack loading when switching a stack fromone thread to another thread. An external process need not capture afull snapshot of a call stack at a single point in time. The externalprocess may traverse each frame at different times, as long as the framehas not been modified since the beginning of the external process. Aframe that has been traversed by a particular external process may beconsidered “safe;” a frame that has not yet been traversed may beconsidered “unsafe.”

One or more embodiments include using conditional branches beforereturning to a calling function and before jumping back to a beginningof a loop to serve multiple purposes. The multiple purposes include, forexample, frame barriers (described above), safepointing, and threadlocal handshakes. A “return condition” is inserted in a functionepilogue to determine whether to return to a calling function or toexecute a slow path. A “loop condition” is inserted in a loop back edgeto determine whether to jump back to a beginning of the loop or toexecute a slow path. Both conditional branches involve evaluations ofthe same thread local value. As described above, a return condition isbased on a comparison between a stack pointer and a thread local value.As an example, a return condition may be that a stack pointer is greaterthan or equal to a thread local value. Additionally, a loop condition isbased on a flag associated with a thread local value. The flag may be,for example, a least significant bit (LSB) of the thread local value. Asan example, a loop condition may be that the LSB of the thread localvalue is equal to zero.

Hence, a return condition and a loop condition, involving evaluations ofthe same thread local value, may serve multiple purposes. A framebarrier may be established by setting the thread local value such that areturn condition would evaluate as true while a loop condition wouldevaluate as false. Meanwhile, a safepoint or thread local handshake maybe established by setting the thread local value such that both thereturn condition and the loop condition would evaluate as true. If noslow paths are required, then the thread local value may be set suchthat both the return condition and the loop condition would evaluate asfalse. If either a return condition or a loop condition is true, thenexecution of a slow path first determines a purpose for the triggeringof the condition, and then proceeds to execute a set of instructionsassociated with the purpose.

Accordingly, a single conditional branch inserted into a functionepilogue is sufficient for achieving multiple purposes. A singleconditional branch inserted before a loop back edge is sufficient forachieving multiple purposes. The conditional branch may control whetherto enter a slow path to establish a frame barrier, safepoint, and/orthread local handshake.

One or more embodiments described in this Specification and/or recitedin the claims may not be included in this General Overview section.

2. Architectural Overview

FIG. 1 illustrates an example architecture in which techniques describedherein may be practiced. Software and/or hardware components describedwith relation to the example architecture may be omitted or associatedwith a different set of functionality than described herein. Softwareand/or hardware components, not described herein, may be used within anenvironment in accordance with one or more embodiments. Accordingly, theexample environment should not be constructed as limiting the scope ofany of the claims.

As illustrated in FIG. 1, a computing architecture 100 includes sourcecode files 101 which are compiled by a compiler 102 into class files 103representing the program to be executed. The class files 103 are thenloaded and executed by an execution platform 112, which includes aruntime environment 113, an operating system 111, and one or moreapplication programming interfaces (APIs) 110 that enable communicationbetween the runtime environment 113 and the operating system 111. Theruntime environment 113 includes a virtual machine 104 comprisingvarious components, such as a memory manager 105 (which may include agarbage collector), a class file verifier 106 to check the validity ofclass files 103, a class loader 107 to locate and build in-memoryrepresentations of classes, an interpreter 108 for executing the virtualmachine 104 code, and a just-in-time (JIT) compiler 109 for producingoptimized machine-level code.

In an embodiment, the computing architecture 100 includes source codefiles 101 that contain code that has been written in a particularprogramming language, such as Java, C, C++, C #, Ruby, Perl, and soforth. Thus, the source code files 101 adhere to a particular set ofsyntactic and/or semantic rules for the associated language. Forexample, code written in Java adheres to the Java LanguageSpecification. However, since specifications are updated and revisedover time, the source code files 101 may be associated with a versionnumber indicating the revision of the specification to which the sourcecode files 101 adhere. The exact programming language used to write thesource code files 101 is generally not critical.

In various embodiments, the compiler 102 converts the source code, whichis written according to a specification directed to the convenience ofthe programmer, to either machine or object code, which is executabledirectly by the particular machine environment, or an intermediaterepresentation (“virtual machine code/instructions”), such as bytecode,which is executable by a virtual machine 104 that is capable of runningon top of a variety of particular machine environments. The virtualmachine instructions are executable by the virtual machine 104 in a moredirect and efficient manner than the source code. Converting source codeto virtual machine instructions includes mapping source codefunctionality from the language to virtual machine functionality thatutilizes underlying resources, such as data structures. Often,functionality that is presented in simple terms via source code by theprogrammer is converted into more complex steps that map more directlyto the instruction set supported by the underlying hardware on which thevirtual machine 104 resides.

In general, programs are executed either as a compiled or an interpretedprogram. When a program is compiled, the code is transformed globallyfrom a first language to a second language before execution. Since thework of transforming the code is performed ahead of time; compiled codetends to have excellent run-time performance. In addition, since thetransformation occurs globally before execution, the code can beanalyzed and optimized using techniques such as constant folding, deadcode elimination, inlining, and so forth. However, depending on theprogram being executed, the startup time can be significant. Inaddition, inserting new code would require the program to be takenoffline, re-compiled, and re-executed. For many dynamic languages (suchas Java) which are designed to allow code to be inserted during theprogram's execution, a purely compiled approach may be inappropriate.When a program is interpreted, the code of the program is readline-by-line and converted to machine-level instructions while theprogram is executing. As a result, the program has a short startup time(can begin executing almost immediately), but the run-time performanceis diminished by performing the transformation on the fly. Furthermore,since each instruction is analyzed individually, many optimizations thatrely on a more global analysis of the program cannot be performed.

In some embodiments, the virtual machine 104 includes an interpreter 108and a JIT compiler 109 (or a component implementing aspects of both),and executes programs using a combination of interpreted and compiledtechniques. For example, the virtual machine 104 may initially begin byinterpreting the virtual machine instructions representing the programvia the interpreter 108 while tracking statistics related to programbehavior, such as how often different sections or blocks of code areexecuted by the virtual machine 104. Once a block of code surpasses athreshold (is “hot”), the virtual machine 104 invokes the JIT compiler109 to perform an analysis of the block and generate optimizedmachine-level instructions which replaces the “hot” block of code forfuture executions. Since programs tend to spend most time executing asmall portion of overall code, compiling just the “hot” portions of theprogram can provide similar performance to fully compiled code, butwithout the start-up penalty. Furthermore, although the optimizationanalysis is constrained to the “hot” block being replaced, there stillexists far greater optimization potential than converting eachinstruction individually. There are a number of variations on the abovedescribed example, such as tiered compiling.

In order to provide clear examples, the source code files 101 have beenillustrated as the “top level” representation of the program to beexecuted by the execution platform 112. Although the computingarchitecture 100 depicts the source code files 101 as a “top level”program representation, in other embodiments the source code files 101may be an intermediate representation received via a “higher level”compiler that processed code files in a different language into thelanguage of the source code files 101. Some examples in the followingdisclosure assume that the source code files 101 adhere to a class-basedobject-oriented programming language. However, this is not a requirementto utilizing the features described herein.

In an embodiment, compiler 102 receives as input the source code files101 and converts the source code files 101 into class files 103 that arein a format expected by the virtual machine 104. For example, in thecontext of the JVM, the Java Virtual Machine Specification defines aparticular class file format to which the class files 103 are expectedto adhere. In some embodiments, the class files 103 contain the virtualmachine instructions that have been converted from the source code files101. However, in other embodiments, the class files 103 may containother structures as well, such as tables identifying constant valuesand/or metadata related to various structures (classes, fields, methods,and so forth).

The following discussion assumes that each of the class files 103represents a respective “class” defined in the source code files 101 (ordynamically generated by the compiler 102/virtual machine 104). However,the aforementioned assumption is not a strict requirement and willdepend on the implementation of the virtual machine 104. Thus, thetechniques described herein may still be performed regardless of theexact format of the class files 103. In some embodiments, the classfiles 103 are divided into one or more “libraries” or “packages”, eachof which includes a collection of classes that provide relatedfunctionality. For example, a library may contain one or more classfiles that implement input/output (I/O) operations, mathematics tools,cryptographic techniques, graphics utilities, and so forth. Further,some classes (or fields/methods within those classes) may include accessrestrictions that limit their use to within a particularclass/library/package or to classes with appropriate permissions.

2.1 Example Class File Structure

FIG. 2 illustrates an example structure for a class file 200 in blockdiagram form according to an embodiment. In order to provide clearexamples, the remainder of the disclosure assumes that the class files103 of the computing architecture 100 adhere to the structure of theexample class file 200 described in this section. However, in apractical environment, the structure of the class file 200 will bedependent on the implementation of the virtual machine 104. Further, oneor more features discussed herein may modify the structure of the classfile 200 to, for example, add additional structure types. Therefore, theexact structure of the class file 200 is not critical to the techniquesdescribed herein. For the purposes of Section 2.1, “the class” or “thepresent class” refers to the class represented by the class file 200.

In FIG. 2, the class file 200 includes a constant table 201, fieldstructures 208, class structures 204, and method structures 209. In anembodiment, the constant table 201 is a data structure which, amongother functions, acts as a symbol table for the class. For example, theconstant table 201 may store data related to the various identifiersused in the source code files 101 such as type, scope, contents, and/orlocation. The constant table 201 has entries for value structures 202(representing constant values of type int, long, double, float, byte,string, and so forth), class information structures 203, name and typeinformation structures 205, field reference structures 206, and methodreference structures 207 derived from the source code files 101 by thecompiler 102. In an embodiment, the constant table 201 is implemented asan array that maps an index i to structure j. However, the exactimplementation of the constant table 201 is not critical.

In some embodiments, the entries of the constant table 201 includestructures which index other constant table 201 entries. For example, anentry for one of the value structures 202 representing a string may holda tag identifying its “type” as string and an index to one or more othervalue structures 202 of the constant table 201 storing char, byte or intvalues representing the ASCII characters of the string.

In an embodiment, field reference structures 206 of the constant table201 hold an index into the constant table 201 to one of the classinformation structures 203 representing the class defining the field andan index into the constant table 201 to one of the name and typeinformation structures 205 that provides the name and descriptor of thefield. Method reference structures 207 of the constant table 201 hold anindex into the constant table 201 to one of the class informationstructures 203 representing the class defining the method and an indexinto the constant table 201 to one of the name and type informationstructures 205 that provides the name and descriptor for the method. Theclass information structures 203 hold an index into the constant table201 to one of the value structures 202 holding the name of theassociated class.

The name and type information structures 205 hold an index into theconstant table 201 to one of the value structures 202 storing the nameof the field/method and an index into the constant table 201 to one ofthe value structures 202 storing the descriptor.

In an embodiment, class structures 204 includes metadata for the class,such as version number(s), number of entries in the constant pool,number of fields, number of methods, access flags (whether the class ispublic, private, final, abstract, etc.), an index to one of the classinformation structures 203 of the constant table 201 that identifies thepresent class, an index to one of the class information structures 203of the constant table 201 that identifies the superclass (if any), andso forth.

In an embodiment, the field structures 208 represent a set of structuresthat identifies the various fields of the class. The field structures208 store, for each field of the class, accessor flags for the field(whether the field is static, public, private, final, etc.), an indexinto the constant table 201 to one of the value structures 202 thatholds the name of the field, and an index into the constant table 201 toone of the value structures 202 that holds a descriptor of the field.

In an embodiment, the method structures 209 represent a set ofstructures that identifies the various methods of the class. The methodstructures 209 store, for each method of the class, accessor flags forthe method (e.g. whether the method is static, public, private,synchronized, etc.), an index into the constant table 201 to one of thevalue structures 202 that holds the name of the method, an index intothe constant table 201 to one of the value structures 202 that holds thedescriptor of the method, and the virtual machine instructions thatcorrespond to the body of the method as defined in the source code files101.

In an embodiment, a descriptor represents a type of a field or method.For example, the descriptor may be implemented as a string adhering to aparticular syntax. While the exact syntax is not critical, a fewexamples are described below.

In an example where the descriptor represents a type of the field, thedescriptor identifies the type of data held by the field. In anembodiment, a field can hold a basic type, an object, or an array. Whena field holds a basic type, the descriptor is a string that identifiesthe basic type (e.g., “B”=byte, “C”=char, “D”=double, “F”=float,“I”=int, “J”=long int, etc.). When a field holds an object, thedescriptor is a string that identifies the class name of the object(e.g. “L ClassName”). “L” in this case indicates a reference, thus “LClassName” represents a reference to an object of class ClassName. Whenthe field is an array, the descriptor identifies the type held by thearray. For example, “[B” indicates an array of bytes, with “[”indicating an array and “B” indicating that the array holds the basictype of byte. However, since arrays can be nested, the descriptor for anarray may also indicate the nesting. For example, “[[L ClassName”indicates an array where each index holds an array that holds objects ofclass ClassName. In some embodiments, the ClassName is fully qualifiedand includes the simple name of the class, as well as the pathname ofthe class. For example, the ClassName may indicate where the file isstored in the package, library, or file system hosting the class file200.

In the case of a method, the descriptor identifies the parameters of themethod and the return type of the method. For example, a methoddescriptor may follow the general form“({ParameterDescriptor})ReturnDescriptor”, where the{ParameterDescriptor} is a list of field descriptors representing theparameters and the ReturnDescriptor is a field descriptor identifyingthe return type. For instance, the string “V” may be used to representthe void return type. Thus, a method defined in the source code files101 as “Object m(int I, double d, Thread t) { . . . }” matches thedescriptor “(I D L Thread) L Object”.

In an embodiment, the virtual machine instructions held in the methodstructures 209 include operations which reference entries of theconstant table 201. Using Java as an example, consider the followingclass:

class A {   int add12and13( ) {     return B.addTwo(12, 13);   } }

In the above example, the Java method add12and13 is defined in class A,takes no parameters, and returns an integer. The body of methodadd12and13 calls static method addTwo of class B which takes theconstant integer values 12 and 13 as parameters, and returns the result.Thus, in the constant table 201, the compiler 102 includes, among otherentries, a method reference structure that corresponds to the call tothe method B.addTwo. In Java, a call to a method compiles down to aninvoke command in the bytecode of the JVM (in this case invokestatic asaddTwo is a static method of class B). The invoke command is provided anindex into the constant table 201 corresponding to the method referencestructure that identifies the class defining addTwo “B”, the name ofaddTwo “addTwo”, and the descriptor of addTwo “(I I)I”. For example,assuming the aforementioned method reference is stored at index 4, thebytecode instruction may appear as “invokestatic #4”.

Since the constant table 201 refers to classes, methods, and fieldssymbolically with structures carrying identifying information, ratherthan direct references to a memory location, the entries of the constanttable 201 are referred to as “symbolic references”. One reason thatsymbolic references are utilized for the class files 103 is because, insome embodiments, the compiler 102 is unaware of how and where theclasses will be stored once loaded into the runtime environment 113. Aswill be described in Section 2.3, eventually the run-timerepresentations of the symbolic references are resolved into actualmemory addresses by the virtual machine 104 after the referenced classes(and associated structures) have been loaded into the runtimeenvironment 113 and allocated concrete memory locations.

2.2 Example Virtual Machine Architecture

FIG. 3 illustrates an example virtual machine memory layout 300 in blockdiagram form according to an embodiment. In order to provide clearexamples, the remaining discussion will assume that the virtual machine104 adheres to the virtual machine memory layout 300 depicted in FIG. 3.In addition, although components of the virtual machine memory layout300 may be referred to as memory “areas”, there is no requirement thatthe memory areas are contiguous.

In the example illustrated by FIG. 3, the virtual machine memory layout300 is divided into a shared area 301 and a thread area 307. The sharedarea 301 represents an area in memory where structures shared among thevarious threads executing on the virtual machine 104 are stored. Theshared area 301 includes a heap 302 and a per-class area 303. In anembodiment, the heap 302 represents the run-time data area from whichmemory for class instances and arrays is allocated. In an embodiment,the per-class area 303 represents the memory area where the datapertaining to the individual classes are stored. In an embodiment, theper-class area 303 includes, for each loaded class, a run-time constantpool 304 representing data from the constant table 201 of the class,field and method data 306 (for example, to hold the static fields of theclass), and the method code 305 representing the virtual machineinstructions for methods of the class.

The thread area 307 represents a memory area where structures specificto individual threads are stored. In FIG. 3, the thread area 307includes thread structures 308 and thread structures 311, representingthe per-thread structures utilized by different threads. In order toprovide clear examples, the thread area 307 depicted in FIG. 3 assumestwo threads are executing on the virtual machine 104. However, in apractical environment, the virtual machine 104 may execute any arbitrarynumber of threads, with the number of thread structures scaledaccordingly.

In an embodiment, thread structures 308 includes program counter 309 andvirtual machine stack 310. Similarly, thread structures 311 includesprogram counter 312 and virtual machine stack 313. In an embodiment,program counter 309 and program counter 312 store the current address ofthe virtual machine instruction being executed by their respectivethreads.

Thus, as a thread steps through the instructions, the program countersare updated to maintain an index to the current instruction. In anembodiment, virtual machine stack 310 and virtual machine stack 313 eachstore frames for their respective threads that hold local variables andpartial results, and is also used for method invocation and return.

In an embodiment, a frame is a data structure used to store data andpartial results, return values for methods, and perform dynamic linking.A new frame is created each time a method is invoked. A frame isdestroyed when the method that caused the frame to be generatedcompletes. Thus, when a thread performs a method invocation, the virtualmachine 104 generates a new frame and pushes that frame onto the virtualmachine stack associated with the thread.

When the method invocation completes, the virtual machine 104 passesback the result of the method invocation to the previous frame and popsthe current frame off of the stack. In an embodiment, for a giventhread, one frame is active at any point. This active frame is referredto as the current frame, the method that caused generation of thecurrent frame is referred to as the current method, and the class towhich the current method belongs is referred to as the current class.

FIG. 4 illustrates an example frame 400 in block diagram form accordingto an embodiment. In order to provide clear examples, the remainingdiscussion will assume that frames of virtual machine stack 310 andvirtual machine stack 313 adhere to the structure of frame 400.

In an embodiment, frame 400 includes local variables 401, operand stack402, and run-time constant pool reference table 403. In an embodiment,the local variables 401 are represented as an array of variables thateach hold a value, for example, Boolean, byte, char, short, int, float,or reference. Further, some value types, such as longs or doubles, maybe represented by more than one entry in the array. The local variables401 are used to pass parameters on method invocations and store partialresults. For example, when generating the frame 400 in response toinvoking a method, the parameters may be stored in predefined positionswithin the local variables 401, such as indexes 1-N corresponding to thefirst to Nth parameters in the invocation.

In an embodiment, the operand stack 402 is empty by default when theframe 400 is created by the virtual machine 104. The virtual machine 104then supplies instructions from the method code 305 of the currentmethod to load constants or values from the local variables 401 onto theoperand stack 402. Other instructions take operands from the operandstack 402, operate on them, and push the result back onto the operandstack 402. Furthermore, the operand stack 402 is used to prepareparameters to be passed to methods and to receive method results. Forexample, the parameters of the method being invoked could be pushed ontothe operand stack 402 prior to issuing the invocation to the method. Thevirtual machine 104 then generates a new frame for the method invocationwhere the operands on the operand stack 402 of the previous frame arepopped and loaded into the local variables 401 of the new frame. Whenthe invoked method terminates, the new frame is popped from the virtualmachine stack and the return value is pushed onto the operand stack 402of the previous frame.

In an embodiment, the run-time constant pool reference table 403contains a reference to the run-time constant pool 304 of the currentclass. The run-time constant pool reference table 403 is used to supportresolution. Resolution is the process whereby symbolic references in theconstant pool 304 are translated into concrete memory addresses, loadingclasses as necessary to resolve as-yet-undefined symbols and translatingvariable accesses into appropriate offsets into storage structuresassociated with the run-time location of these variables.

2.3 Loading, Linking, and Initializing

In an embodiment, the virtual machine 104 dynamically loads, links, andinitializes classes. Loading is the process of finding a class with aparticular name and creating a representation from the associated classfile 200 of that class within the memory of the runtime environment 113.For example, creating the run-time constant pool 304, method code 305,and field and method data 306 for the class within the per-class area303 of the virtual machine memory layout 300. Linking is the process oftaking the in-memory representation of the class and combining it withthe run-time state of the virtual machine 104 so that the methods of theclass can be executed. Initialization is the process of executing theclass constructors to set the starting state of the field and methoddata 306 of the class and/or create class instances on the heap 302 forthe initialized class.

The following are examples of loading, linking, and initializingtechniques that may be implemented by the virtual machine 104. However,in many embodiments the steps may be interleaved, such that an initialclass is loaded, then during linking a second class is loaded to resolvea symbolic reference found in the first class, which in turn causes athird class to be loaded, and so forth. Thus, progress through thestages of loading, linking, and initializing can differ from class toclass. Further, some embodiments may delay (perform “lazily”) one ormore functions of the loading, linking, and initializing process untilthe class is actually required. For example, resolution of a methodreference may be delayed until a virtual machine instruction invokingthe method is executed. Thus, the exact timing of when the steps areperformed for each class can vary greatly between implementations.

To begin the loading process, the virtual machine 104 starts up byinvoking a class loader 107 which loads an initial class. Examples ofclass loaders 107 include a boot class loader, an extension classloader, and an application class loader. The technique by which theinitial class is specified will vary from embodiment to embodiment. Forexample, one technique may have the virtual machine 104 accept a commandline argument on startup that specifies the initial class.

To load a class, the class loader 107 parses the class file 200corresponding to the class and determines whether the class file 200 iswell-formed (meets the syntactic expectations of the virtual machine104). If not, the class loader 107 generates an error. For example, inJava the error might be generated in the form of an exception which isthrown to an exception handler for processing. Otherwise, the classloader 107 generates the in-memory representation of the class byallocating the run-time constant pool 304, method code 305, and fieldand method data 306 for the class within the per-class area 303.

In some embodiments, when the class loader 107 loads a class, the classloader 107 also recursively loads the super-classes of the loaded class.For example, the virtual machine 104 may ensure that the super-classesof a particular class are loaded, linked, and/or initialized beforeproceeding with the loading, linking and initializing process for theparticular class.

During linking, the virtual machine 104 verifies the class, prepares theclass, and performs resolution of the symbolic references defined in therun-time constant pool 304 of the class.

To verify the class, the virtual machine 104 checks whether thein-memory representation of the class is structurally correct. Forexample, the virtual machine 104 may check that each class except thegeneric class Object has a superclass, check that final classes have nosub-classes and final methods are not overridden, check whether constantpool entries are consistent with one another, check whether the currentclass has correct access permissions for classes/fields/structuresreferenced in the constant pool 304, check that the virtual machine 104code of methods will not cause unexpected behavior (e.g. making sure ajump instruction does not send the virtual machine 104 beyond the end ofthe method), and so forth. The exact checks performed duringverification are dependent on the implementation of the virtual machine104. In some cases, verification may cause additional classes to beloaded, but does not necessarily require those classes to also be linkedbefore proceeding. For example, assume Class A contains a reference to astatic field of Class B. During verification, the virtual machine 104may check Class B to ensure that the referenced static field actuallyexists, which might cause loading of Class B, but not necessarily thelinking or initializing of Class B. However, in some embodiments,certain verification checks can be delayed until a later phase, such asbeing checked during resolution of the symbolic references. For example,some embodiments may delay checking the access permissions for symbolicreferences until those references are being resolved.

To prepare a class, the virtual machine 104 initializes static fieldslocated within the field and method data 306 for the class to defaultvalues. In some cases, setting the static fields to default values maynot be the same as running a constructor for the class. For example, theverification process may zero out or set the static fields to valuesthat the constructor would expect those fields to have duringinitialization.

During resolution, the virtual machine 104 dynamically determinesconcrete memory address from the symbolic references included in therun-time constant pool 304 of the class. To resolve the symbolicreferences, the virtual machine 104 utilizes the class loader 107 toload the class identified in the symbolic reference (if not alreadyloaded). Once loaded, the virtual machine 104 has knowledge of thememory location within the per-class area 303 of the referenced classand its fields/methods. The virtual machine 104 then replaces thesymbolic references with a reference to the concrete memory location ofthe referenced class, field, or method. In an embodiment, the virtualmachine 104 caches resolutions to be reused in case the sameclass/name/descriptor is encountered when the virtual machine 104processes another class. For example, in some cases, class A and class Bmay invoke the same method of class C. Thus, when resolution isperformed for class A, that result can be cached and reused duringresolution of the same symbolic reference in class B to reduce overhead.

In some embodiments, the step of resolving the symbolic referencesduring linking is optional. For example, an embodiment may perform thesymbolic resolution in a “lazy” fashion, delaying the step of resolutionuntil a virtual machine instruction that requires the referencedclass/method/field is executed.

During initialization, the virtual machine 104 executes the constructorof the class to set the starting state of that class. For example,initialization may initialize the field and method data 306 for theclass and generate/initialize any class instances on the heap 302created by the constructor. For example, the class file 200 for a classmay specify that a particular method is a constructor that is used forsetting up the starting state. Thus, during initialization, the virtualmachine 104 executes the instructions of that constructor.

In some embodiments, the virtual machine 104 performs resolution onfield and method references by initially checking whether thefield/method is defined in the referenced class. Otherwise, the virtualmachine 104 recursively searches through the super-classes of thereferenced class for the referenced field/method until the field/methodis located, or the top-level superclass is reached, in which case anerror is generated.

3. Garbage Collection

FIG. 5 illustrates an example garbage collection system for processingobjects and/or items in a heap, in accordance with an embodiment. FIG. 5includes garbage collector (GC) 502, GC threads 504 a-b, processingstacks 506 a-b, entries 508 a-b, heap 510, object graph 512, rootreferences 514, live objects 516, and unused objects 518.

As described above, a heap 510 (or heap 302 described above) representsthe run-time data area from which memory for class instances and arraysis allocated. The heap 510 stores objects that are created duringexecution of a program. The heap 510 stores both live objects 516 andunused objects 518.

An object stored in a heap 510 may be a normal object, an object array,or another type of object. A normal object is a class instance. A classinstance is explicitly created by a class instance creation expression.An object array is a container object that holds a fixed number ofvalues of a single type. The object array is a particular set of normalobjects. An object array is explicitly created by an array creationexpression. The length of the object array is established when theobject array is created and is thereafter fixed. Each element or item inthe object array is accessed by a numerical index, beginning with theindex zero (0). As an example, a program may include the followingpseudocode:

-   -   String myobject=“11/15/2011”;    -   String[ ] myarray=new String[2];    -   myarray[0]=“11/16/2011”;    -   myarray[1]=“11/17/2011”;

In this example, myobject is a normal object whose type is String andwhose value is “11/15/2011.” Further, myarray is an object array thatholds two elements, each of which is associated with a type ofString.myarray is a set of two normal object references whose type isString. The value of the object referenced by the first element is“11/16/2011.” The value of the object referenced by the second elementis “11/17/2011.”

In an embodiment, an object graph 512 is a graph including nodes andedges. A node represents a live object 516. An edge represents areference from one live object 516 to another live object 516. The rootnodes of the object graph 512 include objects pointed to by rootreferences 514. The remaining nodes of the object graph 512 includeobjects pointed to by another live object.

In an embodiment, a root reference 514 is a pointer to an object fromwhich a traversal of an object graph 512 begins. A garbage collector 502begins traversing an object graph 512 at a particular object referencedby a root reference 514. The garbage collector 502 identifies theparticular object as a live object 516. The garbage collector 502 tracesthe particular object to identify other objects referenced by theparticular object. The garbage collector 502 identifies the otherobjects referenced by the particular object as live objects 516. Theroot references 514 used by a garbage collector 502 may be determined byanalyzing registers, global fields, and stack frames at the moment whena garbage collection cycle is triggered. Examples of objects referencedby root references 514 include a class loaded by an application classloader, a live thread (such as thread structures 308-311), and a localvariable or parameter (such as local variables 401 and/or operand stack402).

In an embodiment, a live object 516 (also referred to as an “in useobject”) refers to an object that is currently being referenced by anexecuting program. The program includes at least one reference to thelive object 516. A live object 516 is an object that is reachable froman object referenced by a root reference 514.

In an embodiment, an unused object 518 (also referred to as an“unreferenced object” or a “dead object”) refers to an object that is nolonger referenced by any part of the executing program. The unusedobject 518 may be removed from memory. The memory originally used tostore the unused object 518 may be reclaimed to store new live objects516.

In one or more embodiments, a garbage collector 502 refers to hardwareand/or software configured to identify and remove unused objects 518stored in a heap 510. The garbage collector 502 may perform a garbagecollection cycle at a scheduled interval and/or upon an event trigger.As an example, when a heap (or a region thereof) reaches a thresholdallocation, the garbage collector 502 may perform a garbage collectioncycle to remove unused objects 518 stored at the heap (or a regionthereof).

A garbage collector 502 may include one or more GC threads 504 a-b toperform operations in parallel. A GC thread identifies live objects 516by tracing through an object graph 512. After identifying live objects516, the GC thread performs various operations and/or algorithms toremove unused objects 518. Specific operations performed in removingunused object depends on the type of garbage collector 502 used.

As an example, one type of garbage collector 502 is a copying collector.The copying collector uses at least two separately defined addressspaces of the heap, referred to as a “from-space” and a “to-space.” Thecopying collector identifies live objects 516 stored within an areadefined as a from-space. The copying collector copies the live objects516 to another area defined as a to-space. After all live objects 516are copied from the from-space to the to-space, the area defined as thefrom-space is reclaimed. New memory allocation may begin at the firstlocation of the original from-space.

As another example, another type of garbage collector 502 is amark-and-sweep collector. The mark-and-sweep collector utilizes at leasttwo phases: a mark phase and a sweep phase. During the mark phase, themark-and-sweep collector marks each live object 516 with a “live” bit.The live bit may be, for example, a bit within an object header of thelive object 516. During the sweep phase, the mark-and-sweep collectortraverses the heap to identify all non-marked chunks of consecutivememory address spaces. The mark-and-sweep collector links together thenon-marked chunks into organized free lists. The non-marked chunks arereclaimed. New memory allocation is performed using the free lists. Anew object may be stored in a memory chunk identified from the freelists.

The mark-and-sweep collector may be implemented as a parallel collector.The parallel collector includes multiple GC threads 504 a-b working inparallel through the mark and sweep phases.

Additionally or alternatively, the mark-and-sweep collector may beimplemented as a concurrent collector. At least some operations of aconcurrent collector are performed while the executing program orapplication continues to run. Example stages of a concurrent collectorinclude:

Stage 1: Identify the objects referenced by root references (this is notconcurrent with the executing program)

Stage 2: Mark reachable objects from the objects referenced by the rootreferences (this is concurrent)

Stage 3: Identify objects that have been modified as part of theexecution of the program during Stage 2 (this is concurrent)

Stage 4: Re-mark the objects identified at Stage 3 (this is notconcurrent)

Stage 5: Sweep the heap to obtain free lists and reclaim memory (this isconcurrent)

As another example, another type of garbage collector 502 is a partiallyconcurrent collector that also attempts to compact reclaimed memoryareas. The heap is partitioned into a set of equally sized heap regions,each a contiguous range of virtual memory. The partially concurrentcollector performs a concurrent global marking phase to determine theliveness of objects throughout the heap. After the marking phasecompletes, the partially concurrent collector identifies regions thatare mostly empty. The partially concurrent collector collects theseregions first, which often yields a large amount of free space. Thepartially concurrent collector concentrates its collection andcompaction activity on the areas of the heap that are likely to be fullof reclaimable objects, that is, garbage. The partially concurrentcollector copies objects from one or more regions of the heap to asingle region on the heap, and in the process both compacts and frees upmemory. This evacuation is performed in parallel on multiprocessors todecrease pause times and increase throughput.

The partially concurrent collector pauses the executing applicationduring one or more stages of the garbage collection cycle. The partiallyconcurrent collector pauses the executing application to copy liveobjects to new regions. Additionally or alternatively, the partiallyconcurrent collector pauses the executing application to identify andmark objects that have been modified, as part of the execution of theprogram, since start of the initial marking. Additionally oralternatively, the partially concurrent collector pauses the executingapplication to perform a cleanup phase, including identifying emptyregions and determining old regions that are candidates for the nextcollection.

A heap may be separated into different regions. A first region storesobjects that have not yet satisfied a criteria for being promoted fromthe first region to a second region; a second region stores objects thathave satisfied the criteria for being promoted from the first region tothe second region. Different types of garbage collectors 502 may be usedfor the different regions. Different GC processes may apply to thedifferent regions. As an example, a heap may include a young generationspace and an old generation space. A copying collector may performgarbage collection for the young generation space. The young generationspace is separated into an Eden space and two survivor spaces. Objectsare initially allocated in the Eden space. A GC cycle is triggered whenthe Eden space is full. Live objects 516 are copied from the Eden spaceto one of the survivor spaces, for example survivor space 1. At the nextGC cycle, live objects 516 in the Eden space are copied to the othersurvivor space, for example survivor space 2. Live objects 516 insurvivor space 1 are also copied to survivor space 2. Each time a liveobject 516 is copied, the live object 516 may be referred to assurviving a GC cycle. When a live object 516 survives at least athreshold number of GC cycles, the live object 516 is promoted from theyoung generation space to the old generation space.

Each GC thread is associated with a processing stack (such as processingstack 506 a or 506 b). As described above, a GC thread identifies liveobjects 516 by tracing through an object graph 512. The GC thread uses aprocessing stack to track the traversal of the object graph 512. Theprocessing stack stores references to objects that (a) have beenidentified as part of the traversal process and (b) have not yet beenprocessed in accordance with a set of garbage collection operations.

A processing stack has a limited number of entries. If a GC threadattempts to store an additional entry in a processing stack after themaximum number of entries has been reached, an error may be generated.Alternatively, if a GC thread attempts to store an additional entry in aprocessing stack after the maximum number of entries has been reached,the GC thread may allocate a new processing stack. In addition, eachentry of the processing stack stores a limited number of bits. As anexample, each entry may store a maximum of 64 bits.

Entries stored on a processing stack, which is associated with aparticular GC thread, may be distributed to other GC threads for loadbalancing purposes. As an example, processing stack 506 a of GC thread504 a may include ten entries. At the same time, processing stack 506 bof GC thread 504 b may include zero entries. Five entries fromprocessing stack 506 a may be distributed to GC thread 504 b. GC thread504 b, rather than GC thread 504 a, processes the entries distributedfrom processing stack 506 a. As a result, each of GC thread 504 a and GCthread 504 b processes five entries. As illustrated in this example, theloads of GC thread 504 a and GC thread 504 b are thereby balanced.

In one or more embodiments, as the GC 502 performs a GC cycle, the GCcharacteristics 522 are detected and/or obtained. GC characteristics 522include both static characteristics (characteristics that are fixed atthe beginning of a VM) and dynamic characteristics (characteristics thatchange during a runtime of a program or application). Static GCcharacteristics include, for example, a maximum memory size of a younggeneration space, a minimum memory size of a young generation space, amaximum memory size of an old generation space, a minimum memory size ofan old generation space, and a ratio of the memory size of a younggeneration space to the memory size of an old generation space. DynamicGC characteristics include, for example, a memory size of a portion of aheap (or a region thereof) that is allocated before a GC cycle; a memorysize of a portion of a heap (or region thereof) that is allocated aftera GC cycle; a number of objects that are promoted from one region toanother region during a GC cycle. A memory size of a portion of a heapthat is allocated before a GC cycle may also be referred to herein as“size of allocated heap before GC.” A memory size of a portion of ayoung generation space within a heap that is allocated before a GC cyclemay also be referred to herein as “size of allocated young generationbefore GC.”

A GC log 520 including GC characteristics 522 is generated. A GC 502 maygenerate the GC log 520 during a runtime of a program or application.Additionally or alternatively, the GC 502 may generate the GC log 520after execution of the program of application has ended.

4. Call Stacks and Function Stages

In one or more embodiments, a call stack (also referred to herein as a“stack”) stores information associated with different functions (and/ordifferent bodies of code) called by a thread in a program. A call stackis used for keeping track of where to return upon completion ofexecution of a particular function (that is, an address of a call sitein a calling function). Additionally or alternatively, a call stack isused for passing arguments from a calling function to a callee function.Additionally or alternatively, a call stack is used for storing localvariables in the context of a particular function.

In one or more embodiments, each function corresponds to a frame on acall stack. When a new function is called, a new frame is pushed ontothe stack. When execution of the function is complete, the frame ispopped from the stack. A function currently being executed may bereferred to as an “active function.” An active function corresponds to aframe at the top of a call stack, which may be referred to as an “activeframe.” Depending on a configuration of a virtual machine, a call stackmay grow in the positive or negative direction. As an example, when anew frame is pushed onto a call stack, a memory address corresponding tothe new frame may be lesser than a memory address corresponding to aframe already on the call stack. The call stack is said to grow in thenegative direction. As another example, when a new frame is pushed ontoa call stack, a memory address corresponding to the new frame may begreater than a memory address corresponding to a frame already on thecall stack. The call stack is said to grow in the positive direction.

In one or more embodiments, a frame pointer references an address withinan active frame that corresponds to the top of the call stack before anylocal variables are stored in that frame. In one or more embodiments, astack pointer references an address that corresponds to the top of thecall stack, as the call stack grows or shrinks. In one or moreembodiments, a stack pointer is maintained in a register. As an example,in an x86_86 machine, a stack pointer is stored in a RSP register.However, a frame pointer may be but is not necessarily maintained in anyregister. In an embodiment, no register stores a value for the framepointer (thereby freeing up a register for other purposes); rather, avalue for the frame pointer (that is, an address within an active framethat corresponds to the top of the call stack before any local variablesare stored in that frame) is computed based on the stack pointer and/orother information associated with the program, such as a programcounter. As an example, based on the program counter, the instructionsthat have been executed may be determined. The local variables that arestored based on the executed instructions may be determined. The size ofthe local variables may be determined. Therefore, the value of the framepointer may be computed as the stack pointer minus the size of the localvariables.

In one or more embodiments, multiple call stacks are concurrently usedin a virtual machine, each call stack associated with a different threadof the virtual machine. The threads in a virtual machine may include,for example, one or more program threads, one or more garbage collection(GC) threads, and one or more stack sampling threads. Additional and/oralternative threads may be used.

In one or more embodiments, each program process and/or thread isassociated with a thread local value (also referred to herein as a“process-specific value”). A thread local value is a value specific to asingle program thread in a virtual machine. Only a particular programthread corresponding to a particular thread local value may read and/orwrite to the particular thread local value; other program threads cannotread and/or write to the particular thread local value. However, othertypes of threads (such as a GC thread) may read and/or write to a threadlocal value of a program thread. In one or more embodiments, a threadlocal value is maintained in a memory location, which is referenced by avalue stored in a register. As an example, in an x86_64 machine, a R15register stores a value that references a memory location in which athread local value is stored. Each thread (in a multi-threaded program)is associated with a respective register state. When switching threads,a value stored in a R15 register of the outgoing thread is saved inmemory, and a value previously saved in memory for the R15 register forthe incoming thread is loaded into the R15 register. In a GC context, aprogram thread may also be referred to as a “mutator thread,” because amutator thread may mutate the contents of a heap as a GC thread attemptsto perform a GC cycle.

In one or more embodiments, overhead instructions, in a functionprologue 602 and/or function epilogue 606, are executed to switchbetween different frames on a call stack. Hence, there may be threestages in the execution of a function: function prologue 602, functionbody 604, and function epilogue 606. The function prologue 602 and/orfunction epilogue 606 constitute procedural functionality forintegrating core code into a body of other code. The core codefunctionality is not for integrating the core code into the body ofother code, but rather to accomplish a function not performed by thebody of other code. FIG. 6 illustrates an example set of stages ofexecution of a function, in accordance with one or more embodiments. Inone or more embodiments, more or fewer stages than the stagesillustrated in FIG. 6 may be used.

In one or more embodiments, a function prologue 602 includesinstructions that prepare a new frame on a call stack for a calleefunction. A function prologue 602 includes, for example, pushing areturn address on the call stack (that is, an address of a call sitewithin a calling function), and shifting a stack pointer to include theinformation pushed onto the call stack thus far (such as, the returnaddress and, optionally, the value of the frame pointer) and memoryreserved for local variables associated with the callee function.

In one or more embodiments, a function body 604 includes instructionsnecessary for achieving a purpose of a function. As an example, apurpose of a function may be to determine a sum of two variables. Afunction body of the function may include instructions that determinesthe respective values of the two variables, and adds the two valuestogether.

In one or more embodiments, a function epilogue 606 includesinstructions that restore a frame on a call stack for a callingfunction. A function epilogue 606 includes instructions performed aftera function body 604 (a functional portion of code) but before a returncall from the callee function. A function epilogue 606 includes, forexample, shifting a stack pointer to be equal a frame pointer (anaddress corresponding to the top of the call stack before localvariables were stored for the active frame), and returning to thecalling site. Additionally, a function epilogue 606 may include pollingprior to returning to the calling site. Polling may include determiningwhether a return condition is satisfied. Polling may include executing aconditional branch, as further described below with reference to FIG. 7.

In one or more embodiments, a first set of code is compiled and/orinterpreted into a second set of code. The first set of code specifies afunction body of a function. The first set of code does not specify anyinstructions that are directly compiled and/or interpreted intoinstructions of the function prologue 602 and function epilogue 606.During compilation and/or interpretation, the compiler and/orinterpreter inserts instructions into the second set of code thatconstitute the function prologue 602 and function epilogue 606.

As an example, the following source code may be executed:

public static void main(String[ ] args) {   System.out.println(“HelloWorld”); }

In the above example, main calls println. Hence, main may be referred toas a “calling function,” and println may be referred to as a “calleefunction.” On a call stack, a frame corresponding to main is firstpushed. When main calls println, a frame corresponding to println ispushed. Execution of a function body of println involves printing “HelloWorld.” After execution of the function body, a function epilogue ofprintln is executed. A function epilogue of println includes operationssuch as moving a stack pointer, moving a frame pointer, polling, andreturning to the calling function main. Reviewing the example sourcecode, the example source code does not include any line corresponding tooperations such as moving a stack pointer, moving a frame pointer,polling, and returning to the calling function main (setting a programcounter to reference an instruction associated with a call site inmain). Hence, as described above, the function epilogue includes codethat is not directly compiled and/or interpreted from the source code;rather the function epilogue is inserted by a compiler and/orinterpreter.

In one or more embodiments, a function may include a loop. A loop is aprogramming structure that repeats a sequence of instructions until aspecific condition is met. A loop body includes instructions that areexecuted if and only if a loop's condition is met. A loop body includesinstructions necessary for achieving a purpose of a loop. A loop body iscompiled and/or interpreted based on source code. A back edge of a loopis a jump back into a previously-executed instruction. One or moreinstructions corresponding a back edge does not correspond to anyinstructions in the source code. The instruction corresponding a backedge are inserted by a compiler and/or interpreter. Optionally, acompiler and/or interpreter may also insert instructions for pollingprior to the back edge instruction. Polling may include determiningwhether a loop condition is satisfied. Polling may include executing aconditional branch, as further described below with reference to FIG.11.

As an example, the following source code may be executed:

public static void main(String[ ] args) {   for (int x=1; x<5; x++) {    System.out.println(“Value of x:” + x);   } }

In the above example, for (int x=1; x<5; x++) may be referred to as aloop header. The loop header specifies the loop condition, which in theabove example is x<5. A loop body is the code within the brackets afterthe loop header, which in the above example is System.out.println(“Value of x:”+x);. Execution of the loop body involves printing thevalue of x. After one iteration of the loop body, polling and back edgeinstructions are executed. The back edge instruction includes a jumpback to the beginning of the loop body. Reviewing the example sourcecode, the example source code does not include any line corresponding tooperations such as polling, and jumping back to the beginning of theloop body (setting a program counter to reference an instruction at thebeginning of the loop body). Hence, as described above, polling and backedge instructions are not directly compiled and/or interpreted from thesource code; rather polling and back edge instructions are inserted by acompiler and/or interpreter.

In some embodiments, polling may be disabled in certain circumstances.As an example, polling may be disabled for loops that have a finitenumber of iterations. As another example, loop strip mining may be usedto optimize execution of a loop. Loop strip mining transforms a loopinto an external loop and an internal loop. Polling may be disabled forthe internal loop. Polling may be allowed on the external loop.Therefore, certain iterations of the loop (the iterations performed bythe internal loop) may be performed without the poll. Meanwhile, otheriterations of the loop (the iterations performed by the external loop)may be performed with the poll.

5. Using a Conditional Branch in a Function Epilogue to ProvideFrame-Specific Control

FIG. 7 illustrates an example set of operations for using a conditionalbranch in a function epilogue to provide frame-specific control, inaccordance with one or more embodiments. Operations of FIG. 7 areexecuted after execution of a function body of an active function. Atthe completion of the execution of the function body of the activefunction, the active frame of the call stack (the frame at the top ofthe call stack) corresponds to the active function. A stack pointerreferences the top of the call stack, which currently includes the localvariables associated with the active function. Optionally, a framepointer references the top of the call stack before the local variablesassociated with the active function were added to the call stack. In oneor more embodiments, operations of FIG. 7 are performed by a programprocess, which may be executed on a same thread or a different threadthan an external process (such as, a garbage collection process).

One or more operations illustrated in FIG. 7 may be modified,rearranged, or omitted all together. Accordingly, the particularsequence of operations illustrated in FIG. 7 should not be construed aslimiting the scope of one or more embodiments.

One or more embodiments include setting a stack pointer to equal a valueof a frame pointer (Operation 702). A value of the frame pointer isidentified. Depending on a configuration of a virtual machine, the framepointer may or may not be maintained in a register. If the frame pointeris maintained in a register, then the value of the frame pointer isretrieved from the register. If the frame pointer is not maintained in aregister, then the value of the frame pointer may be computed based onthe stack pointer and/or other information associated with the program,such as a program counter.

A register corresponding to the stack pointer is identified. As anexample, in an x86_64 machine, a RSP register corresponding to the stackpointer is identified. The value of the frame pointer is stored into theregister. Hence, the stack pointer is set to the value of the framepointer. Both the stack pointer and the frame pointer reference the topof the call stack before local variables associated with the activefunction were added to the call stack.

One or more embodiments include identifying a thread local value(Operation 704). The thread local value is loaded from a register and/oranother memory location. As an example, in an x86_64 machine, a threadlocal value is loaded from a R15 register.

The thread local value may be set by one or more processes and/orthreads for achieving one or more purposes using the conditional branch.Examples of operations for setting the thread local value are furtherdescribed below with reference to FIG. 8.

One or more embodiments include determining whether the stack pointer isgreater than or equal to the thread local value (Operation 706). Pollingis performed as part of a function epilogue. During polling, a returncondition is tested.

Depending on a configuration of a virtual machine, a call stack may growin the positive or negative direction. In an embodiment, the call stackgrows in the negative direction. Hence, a return condition to be testedis whether the stack pointer is greater than or equal to the threadlocal value (as illustrated). In an alternative embodiment, the callstack grows in a positive direction. Hence, a return condition to betested is whether the stack pointer is less than or equal to the threadlocal value.

If the stack pointer is greater than or equal to the thread local value,then one or more embodiments include going to a slow path (Operation708). Examples of operations of a slow path are further described belowwith reference to FIG. 9. After execution of the slow path, the programthread returns to a poll site. Polling is performed to determine whetherthe stack pointer is greater than or equal to the thread local value(Operation 706).

If the stack pointer is not greater than or equal to the thread localvalue, then one or more embodiments include returning to a call site forthe function (Operation 710). The program thread returns to a call sitefor the function, that is, the location of an instruction that calledthe function.

6. Setting a Condition of a Conditional Branch

FIGS. 8A-8C illustrates an example set of operations for setting acondition of a conditional branch, in accordance with one or moreembodiments. Setting a condition of the conditional branch includessetting a thread local value that is used in the conditional branch, asdescribed at Operation 706 of FIG. 7. When setting the thread localvalue, there is no need to wait for a suspension or handshake by theprogram thread. In one or more embodiments, operations of FIGS. 8A-8Care performed by an external process and/or thread that is external to aprogram process associated with the thread local value. The externalprocess and the program process may be executed on a same thread ordifferent threads. The external process requires snapshots of each frameof a call stack associated with the program thread, wherein allsnapshots reflect a state of the call stack at a particular time.Examples of such external processes include stack scanning in garbagecollection (GC), a deoptimization process that replacespreviously-optimized native code, stack sampling for inspecting anexecution of a program, and stack loading when switching a stack fromone program thread to another program thread. An external process neednot capture a full snapshot of a call stack at a single point in time.The external process may traverse each frame at different times, as longas the frame has not been modified since the beginning of the externalprocess.

One or more operations illustrated in FIGS. 8A-8C may be modified,rearranged, or omitted all together. Accordingly, the particularsequence of operations illustrated in FIGS. 8A-8C should not beconstrued as limiting the scope of one or more embodiments.

FIG. 8A illustrates an example set of operations for setting a conditionof a conditional branch to establish a frame barrier for a frame of acall stack.

One or more embodiments include completing traversal of a particularframe of a call stack (Operation 802). An external process traverseseach frame of a call stack. At a particular point in time, traversal ofa subset of frames on a call stack may have been completed, whiletraversal of another subset of frames on the call stack is not yetcompleted.

As an example, a GC thread may need to scan a call stack to identifyroot references. The GC thread may scan the call stack frame by frame.After scanning a particular frame, traversal of the particular frame maybe referred to as completed.

As another example, a GC thread may have reclaimed memory by copyingobjects from one region of memory to another region of memory. The GCthread may need to scan a call stack to remap references to the objectsin the new memory locations. The GC thread may scan the call stack frameby frame. After scanning a particular frame, traversal of the particularframe may be referred to as completed.

As another example, a deoptimization process may need to be performed. AJIT compiler performs speculative optimizations that may becomeinvalidated during runtime. As an example, a JIT compiler may make anassumption about a call being monomorphic. However, concurrent classloading may make the call polymorphic, thereby violating the assumption.When certain optimized code is no longer valid, the code is discarded.The deoptimization process may need to scan a call stack to remap,re-compile, and/or re-interpret code that is no longer valid. Thedeoptimization process may scan the call stack frame by frame. Afterscanning a particular frame, traversal of the particular frame may bereferred to as completed.

As another example, a stack sampling process may need to obtain a sampleof a full call stack. The stack sampling process may be performed by,for example, a Java Flight Recorder. The stack sampling process may scanthe call stack frame by frame. After scanning a particular frame,traversal of the particular frame may be referred to as completed.

As another example, a stack loading process may need to switch executionassociated with a call stack from one fiber to another fiber. A fiber isa user-mode thread managed by the VM (as opposed to a kernel threadimplemented by an operating system). The stack loading process maytraverse the call stack frame by frame, to switch execution associatedwith each frame from one fiber to another fiber. After switchingexecution of a particular frame, traversal of the particular frame maybe referred to as completed.

One or more embodiments include shifting a thread local value toindicate that the particular frame has been traversed (Operation 804).After traversal of the particular frame is completed, the externalprocess identifies a thread local value specific to the program threadassociated with the call stack. The external process shifts the threadlocal value to indicate that the particular frame has been traversed.

In an embodiment, a call stack grows in a negative direction. The framesin a call stack, from bottom of the stack to top of the stack, mayinclude: Frame A, Frame B. At a first time period, an external processmay complete traversal of Frame B, but not of Frame A. The externalprocess may set the thread local value to be (a) less than or equal toan address referenced by a frame pointer associated with Frame B, and(b) greater than an address associated with memory reserved for localvariables of Frame B. Hence, the thread local value indicates that FrameB has been traversed, but Frame A has not been traversed.

In another embodiment, a call stack grows in a positive direction. Theframes in a call stack, from bottom of the stack to top of the stack,may include: Frame A, Frame B. At a first time period, an externalprocess may complete traversal of Frame B, but not of Frame A. Theexternal process may set the thread local value to be (a) greater thanor equal to an address referenced by a frame pointer associated withFrame B, and (b) less than an address associated with memory reservedfor local variables of Frame B. The thread local value indicates thatFrame B has been traversed, but Frame A has not been traversed.

In an embodiment, the external process may also set the thread localvalue such that a least significant bit (LSB) of the thread local valueis one. The LSB of the thread local value serves as a flag to be used ina loop condition, as further described below with reference to Operation1106 of FIG. 11.

As an example, frames on a call stack may be associated with an 8-bytealignment. Hence the three least significant bits for any address of aframe on the call stack is zero. An external process may set the threadlocal value such that (a) the thread local value is less than or equalto an address referenced by a frame pointer associated with a frame thathas been most recently processed, (b) the thread local value is greaterthan an address associated with memory reserved for local variables ofthe frame that has been most recently processed, and (c) the LSB of thethread local value is equal to one. Hence, the thread local value maybe, for example, the address referenced by the frame pointer associatedwith the frame that has been most recently processed minus 7.

One or more embodiments include determining whether there is anadditional frame to traverse (Operation 806). If there is an additionalframe of the call stack to traverse, the external process iteratesOperation 802 with respect to the additional frame. After traversing theadditional frame, the external process iterates Operation 804 to shiftthe thread local value to indicate that the additional frame has beentraversed.

Therefore, the thread local value tracks the progress of the externalprocess. As an external process traverses each frame of a call stack,the external process shifts the thread local value to indicate the framethat has just been traversed. As described above, a return condition atOperation 706 depends on the thread local value. By tracking a progressof an external process using the thread local value, the returncondition indicates whether an active function is returning to a framethat has been traversed or not. If the active function is returning to aframe that has not been traversed, then there is a jump to a slow path.If the active function is returning to a frame that has been traversed,then there is a return to the frame.

FIG. 8B illustrates an example set of operations for setting a conditionof a conditional branch to establish a safepoint.

One or more embodiments include determining that a safepoint is needed(Operation 808). An external process determines that a safepoint isneeded. A safepoint is a suspension of execution of a program by allprogram threads. As an example, a GC thread may perform a particularoperation that requires execution of a program by all threads to besuspended.

Further embodiments and examples relating to safepoints are described in“Safepoint,” in HotSpot Glossary of Terms, athttp://openjdk.java.net/groups/hotspot/docs/HotSpotGlossary.html (lastaccessed on Oct. 12, 2018), included herein as Appendix A, andincorporated by reference herein.

One or more embodiments include setting a thread local value to requirea slow path (Operation 810). In an embodiment, a call stack grows in anegative direction. The external process sets the thread local value toa minimum value (all 0's). In another embodiment, a call stack grows ina positive direction. The external process sets the thread local valueto a maximum value (all 1's).

As described above, a return condition at Operation 706 depends on thethread local value. By setting the thread local value such that thereturn condition always evaluates as true, a slow path is executed.

In order to establish a safepoint, a thread local value of each programthread in a VM is set to require the slow path. All program threadsexecute the slow path in order to suspend execution of the program untilthe external process traverses all program threads.

FIG. 8C illustrates an example set of operations for setting a conditionof a conditional branch to establish a thread local handshake.

One or more embodiments include determining that a thread localhandshake is needed (Operation 812). An external process determines thata thread local handshake is needed. A thread local handshake (alsoreferred to as a “process-specific handshake”) is a suspension ofexecution of a program by a particular program process and/or thread. Asan example, a GC thread may perform a particular operation that requiresexecution of a program by a particular thread to be suspended.

Further embodiments and examples relating to thread local handshakes aredescribed in JEP 312: Thread Local Handshakes, athttp://openjdk.java.net/jeps/312 (last accessed on Oct. 12, 2018),included herein as Appendix B, and incorporated by reference herein.

One or more embodiments include setting a thread local value to requirea slow path (Operation 814). In an embodiment, a call stack grows in anegative direction. The external process sets the thread local value toa minimum value (all 0's). In another embodiment, a call stack grows ina positive direction. The external process sets the thread local valueto a maximum value (all 1's).

As described above, a return condition at Operation 706 depends on thethread local value. By setting the thread local value such that thereturn condition always evaluates as true, a slow path is executed.

The external process may determine whether to establish a thread localhandshake for each program thread individually. As an example, anexternal process may initially establish a thread local handshake foreach program thread of a virtual machine. The external process may setthe thread local values of the program threads as the minimum value (all0's). When the external process completes traversal of a particularprogram thread, the external process may release the thread localhandshake of the particular program thread. The external process maymodify the thread local value associated with the particular programthread. The external process may set the thread local value associatedwith the particular program thread to the maximum value (all 1's) oranother value. Meanwhile, the thread local values of the other programthreads, which have not yet been traversed, may remain the same.

7. Performing Various Controls on Execution of a Thread, IncludingFrame-Specific Control

FIG. 9 illustrates an example set of operations for performing variouscontrols on execution of a thread, including frame-specific control, inaccordance with one or more embodiments. After determining to enter aslow path at Operation 708 of FIG. 7, operations of FIG. 9 are executed.In one or more embodiments, operations of FIG. 9 are performed by aprogram process, which may be executed on a same thread or a differentthread than an external process (such as, a garbage collection process).

One or more operations illustrated in FIG. 9 may be modified,rearranged, or omitted all together. Accordingly, the particularsequence of operations illustrated in FIG. 9 should not be construed aslimiting the scope of one or more embodiments.

One or more embodiments include determining one or more purposes ofentering a slow path (Operation 902). Registers and/or other attributesassociated with a virtual machine may be evaluated to determine apurpose of entering the slow path.

In an embodiment, a global value associated with the virtual machineindicates whether a safepoint is needed. The global value has at leastthree possible states: synchronizing, synchronized, not synchronized. Ifthe global value indicates the “synchronized” or “synchronizing” state,then yielding to the safepoint and waiting for the safepoint to finishis required.

In an embodiment, each program thread is associated with a “handshakeoperation” field. If the “handshake operation” field is NULL, there isno handshake to reply to. If there is an operation in the “handshakeoperation” field, then the operation is executed. A thread localhandshake is needed during the execution of the operation.

In an embodiment, when jumping into the virtual machine from a slowpath, a “last frame” is recorded. If the thread local value is not theminimum value (all 0's) nor the maximum value (all 1's), then the threadlocal value is compared with the “last frame” to determine whether the“last frame” is unsafe. If the “last frame” is unsafe, then a framebarrier is needed until the “last frame” is safe.

If a frame barrier is needed, one or more embodiments includeestablishing a frame barrier (Operation 904). Examples of operations forestablishing a frame barrier are further described below with referenceto FIG. 10.

If a thread local handshake is needed, one or more embodiments includeestablishing a thread local handshake (Operation 906). An operationindicated in the “handshake operation” field is executed. The programthread itself and/or an external process may execute the handshakeoperation. During execution, the thread local value remains the same asthe value set at Operation 814. After the call stack associated with theprogram thread is traversed by the handshake operation, the thread localvalue is modified to indicate that the thread local handshake is nolonger needed for the program thread.

In an embodiment, whether a program thread or an external processexecutes the handshake operation depends upon which mode the programthread currently operates. Examples of modes include managed mode andunmanaged mode. A program thread is in managed mode when the programthread executes code that is interpreted from source code. A programthread is in unmanaged mode when the program thread executes native Ccode or the program thread is associated with a particular lock. When aprogram thread is in managed mode, the program thread itself (ratherthan the external process) executes the handshake operation. When aprogram thread is in unmanaged mode, the external process executes thehandshake operation, without necessarily waiting for the program threadto return to managed mode. When the program thread is in unmanaged mode,the external process may safely execute the handshake operation for theprogram thread because, if and when the program thread wakes up tomanaged mode, the program thread would run into the poll. Running intothe poll would make the program thread wait until the external processhas completed the handshake operation. In other embodiments, whether aprogram thread or an external process executes the handshake operationdepends upon additional and/or alternative factors.

If a safepoint is needed, one or more embodiments include establishing asafepoint (Operation 908). The program thread itself and/or an externalprocess may execute a particular operation on call stacks of the virtualmachine. The particular operation requires and/or triggers thesafepoint. During execution, the thread local value remains the same asthe value set at Operation 810. After every call stack is traversed, thethread local value is modified to indicate that the safepoint is nolonger needed.

In an embodiment, whether a program thread or an external processexecutes the particular operation depends upon which mode the programthread currently operates. When a program thread is in managed mode, theprogram thread itself (rather than the external process) executes theparticular operation. When a program thread is in unmanaged mode, theexternal process executes the particular operation, without necessarilywaiting for the program thread to return to managed mode. In otherembodiments, whether a program thread or an external process executesthe particular operation depends upon additional and/or alternativefactors.

One or more embodiments include returning to a poll site (Operation910). After the operations causing the need for the frame barrier, thethread local handshake, or the safepoint are executed, then there isreturn to the poll site. The program thread returns to Operation 706.The return condition is checked again, using the current thread localvalue.

8. Handling a Frame Barrier

FIG. 10 illustrates an example set of operations for handling a framebarrier, in accordance with one or more embodiments. One or moreoperations illustrated in FIG. 10 may be modified, rearranged, oromitted all together. Accordingly, the particular sequence of operationsillustrated in FIG. 10 should not be construed as limiting the scope ofone or more embodiments.

One or more embodiments include completing traversal of a frame fromwhich a frame barrier blocks entry (Operation 1002). A program threadand/or an external process traverses a frame from which a frame barrierblocks entry.

In an embodiment, an external process traverses the frame from which theframe barrier blocks entry. Examples of operations for traversing, by anexternal process, a frame are described above with reference toOperation 802.

In another embodiment, a program thread may traverse the frame, usingoperations similar to those described above with reference to Operation802. As an example, references in a call stack may need to be remappedas part of a GC cycle. A program thread may perform the remapping ofreferences for a particular frame. As another example, concurrentmarking may need to be performed. A program thread may mark thereferences in a particular frame. As another example, objects may needto be relocated. A program thread may move the objects referenced by aparticular frame and remap pointers to reference the new locations ofthe objects.

In an embodiment, whether a program thread or an external processtraverses the frame from which the frame barrier blocks entry dependsupon which mode the program thread currently operates. When a programthread is in managed mode, the program thread itself (rather than theexternal process) traverses the frame. When a program thread is inunmanaged mode, the external process traverses the frame, withoutnecessarily waiting for the program thread to return to managed mode. Inother embodiments, whether a program thread or an external processtraverses the frame from which the frame barrier blocks entry dependsupon additional and/or alternative factors.

One or more embodiments include shifting a thread local value toindicate that the frame has been traversed (Operation 1004). The programthread and/or the external process shifts the thread local value toindicate that the frame has been traversed. The thread local value maybe shifted even if the entire call stack has not yet been traversed.

Examples of operations for shifting, by the external process, a threadlocal value to indicate that a frame has been traversed are describedabove with reference to Operation 804.

In an embodiment, a program thread may shift the thread local value toindicate that the frame has been traversed, using operations similar tothose described above with reference to Operation 804.

9. Using a Conditional Branch Before a Back Edge of a Loop

FIG. 11 illustrates an example set of operations for using a conditionalbranch before a back edge of a loop, in accordance with one or moreembodiments. Operations of FIG. 11 are executed after execution of aloop body. As described above, polling and back edge instructions areperformed after execution of a loop body.

In one or more embodiments, operations of FIG. 11 are performed by aprogram process, which may be executed on a same thread or a differentthread than an external process (such as, a garbage collection process).

One or more operations illustrated in FIG. 11 may be modified,rearranged, or omitted all together. Accordingly, the particularsequence of operations illustrated in FIG. 11 should not be construed aslimiting the scope of one or more embodiments.

One or more embodiments include identifying a thread local value(Operation 1104). Examples of operations for identifying a thread localvalue are described above with reference to Operation 704.

One or more embodiments include determining whether a least significantbit (LSB) of the thread local value is equal to zero (Operation 1106).In an embodiment, the LSB of the thread local value serves as a flag fora loop condition. In another embodiment, a different bit of the threadlocal value may be used as a flag.

As described above with reference to FIGS. 8A-8C, when a safepoint orthread local handshake is needed, the thread local value is set to aminimum value (all 0's). Hence, the loop condition would return true.When a frame barrier is needed, the thread local value is set such thatthe LSB is 1. Hence, the loop condition would return false.

If the LSB of the thread local value is equal to zero, then one or moreembodiments include going to a slow path (Operation 1108). Examples ofoperations of a slow path are further described below with reference toFIG. 9. After execution of the slow path, the program thread returns toa poll site. Polling is performed to determine whether an LSB of thethread local value is equal to zero (Operation 1106).

If the LSB of the thread local value is not equal to zero, then one ormore embodiments include restarting the loop (Operation 1110). Theprogram thread returns to the location of a beginning instruction forthe loop.

10. Example Embodiment

A detailed example is described below for purposes of clarity.Components and/or operations described below should be understood as onespecific example which may not be applicable to certain embodiments.Accordingly, components and/or operations described below should not beconstrued as limiting the scope of any of the claims.

FIGS. 12A-12D illustrate an example conditional branch frame barrier, inaccordance with one or more embodiments.

Referring to FIG. 12A, call stack 1200 is associated with a programthread. Frame 1206 a, associated with Function A, is first pushed ontocall stack 1200. Frame 1206 b, associated with Function B, is thenpushed onto call stack 1200.

Frame 1206 a includes parameters 1208 a for Function A, a return address1210 a to be used when exiting from Function A, and local variables 1212a associated with Function A. Frame 1206 b includes parameters 1208 bfor Function B, a return address 1210 b to be used when exiting fromFunction B, and local variables 1212 b associated with Function B.

Stack pointer 1202 references a top of call stack 1200, which includeslocal variables 1212 b of the active function, Function B. Frame pointer1204 references a top of call stack 1200 before local variables 1212 bwere stored.

A GC thread is performing a GC cycle. The GC thread needs to traverseall frames on call stack 1200. As illustrated in FIG. 12A, frame 1206 b(shaded in grey) has been traversed, while frame 1206 a has not yet beentraversed. The GC thread sets thread local value 1214 to indicate thatframe 1206 b has been traversed but frame 1206 a has not yet beentraversed. In particular, the GC thread sets thread local value 1214 tobe equal to the address referenced by frame pointer 1204 minus seven.

Referring to FIG. 12B, at a later time, execution of a function body ofFunction B is completed. During a function epilogue of Function B, stackpointer 1202 is set to a value of frame pointer 1204.

During the function epilogue, polling is conducted. Specifically, areturn condition is checked. The return condition compares stack pointer202 and thread local value 1214. If stack pointer 202 is greater than orequal to thread local value 1214, then a slow path is entered. In thecurrent scenario, stack pointer 1202 is greater than or equal to threadlocal value 1214 (as illustrated, stack pointer 1202 is above threadlocal value 1214). Therefore, the slow path is entered.

Referring to FIG. 12C, during execution of the slow path, the GC threadtraverses frame 1206 a (now also shaded in grey). The GC thread setsthread local value 1214 to indicate that frame 1206 a has beentraversed. In particular, the GC thread sets thread local value 1214 tobe equal to the address referenced by a frame pointer associated withframe 1206 a minus seven.

The program thread exits the slow path and returns to the poll site. Theprogram thread again checks the return condition. The return conditioncompares stack pointer 202 and thread local value 1214. In the currentscenario, stack pointer 1202 is not greater than or equal to threadlocal value 1214 (as illustrated, stack pointer 1202 is below threadlocal value 1214). Therefore, the slow path is not entered. The programthread returns to a call site for Function B.

Referring to FIG. 12D, frame 1206 b is popped from call stack 1200.Stack pointer 1204 references a top of call stack 1200, which includeslocal variables 1212 a of the active function, Function A. Frame pointer1204 references a top of call stack 1200 before local variables 1212 awere stored.

Another detailed example is described below for purposes of clarity.Components and/or operations described below should be understood as onespecific example which may not be applicable to certain embodiments.Accordingly, components and/or operations described below should not beconstrued as limiting the scope of any of the claims.

Source code for a loop may be as follows:

for (int x=0; x<n; x++) {   System.out.println(x); }

Machine code for a loop may be as follows:

mov edx, 0 # edx is “x” movabs rsi, 0x643634 # address of System.out inheap loop: cmp edx, n jz loopend push edx call printli pop edx inc edx[polling: check a loop condition] jmp loop loopend:

Initially, x is 0. At the first iteration of the loop, printli iscalled, and “0” is printed. The program counter reaches the pollinginstructions. A loop condition is checked. The loop condition checkswhether a least significant bit (LSB) of a thread local value is equalto 0. The thread local value is identified. The LSB of the thread localvalue may be equal to 1. Therefore, the loop condition is not satisfied.Hence, the program counter reaches the back edge instructions, whichrequire the program counter to jump back to the beginning of the loop.

Now, x is 1. At the second iteration of the loop, printli is called, and“1” is printed. At this time, a safepoint is needed. The thread localvalue is set to all 0's, and hence the LSB of the thread local value is0. Referring back to the execution of the loop, the program counterreaches the polling instructions before the back edge. The loopcondition is checked. The loop condition checks whether the LSB of thethread local value is equal to 0. The thread local value is identified.As stated above, the LSB of the thread local value is now equal to 0.Therefore, the loop condition is satisfied. Hence, the program processtakes a slow path.

The slow path may involve the program thread and/or an external processperforming operations triggering and/or requiring the safepoint. Afterthe operations are complete, the thread local value is set to all 1's.The program thread returns to the poll site. The loop condition is againchecked. Since the LSB of the thread local value is 1, the loopcondition is not satisfied. Hence, the program counter reaches the backedge instructions, which require the program counter to jump back to thebeginning of the loop.

Now, x is 2. At the second iteration of the loop, printli is called, and“2” is printed. At this time, a garbage collection process has started.The thread local value is set to track a progress of the traversal offrames by the garbage collection process. Additionally, the local threadvalue is set such that the LSB is 1. Referring back to the execution ofthe loop, the program counter reaches the polling instructions beforethe back edge. The loop condition is checked. The loop condition checkswhether the LSB of the thread local value is equal to 0. The threadlocal value is identified. As stated above, the LSB of the thread localvalue is equal to 1. Therefore, the loop condition is not satisfied.Hence, the program counter reaches the back edge instructions, whichrequire the program counter to jump back to the beginning of the loop.

11. Miscellaneous; Extensions

Embodiments are directed to a system with one or more devices thatinclude a hardware processor and that are configured to perform any ofthe operations described herein and/or recited in any of the claimsbelow.

In an embodiment, a non-transitory computer readable storage mediumcomprises instructions which, when executed by one or more hardwareprocessors, causes performance of any of the operations described hereinand/or recited in any of the claims.

Any combination of the features and functionalities described herein maybe used in accordance with one or more embodiments. In the foregoingspecification, embodiments have been described with reference tonumerous specific details that may vary from implementation toimplementation. The specification and drawings are, accordingly, to beregarded in an illustrative rather than a restrictive sense. The soleand exclusive indicator of the scope of the invention, and what isintended by the applicants to be the scope of the invention, is theliteral and equivalent scope of the set of claims that issue from thisapplication, in the specific form in which such claims issue, includingany subsequent correction.

12. Hardware Overview

According to one embodiment, the techniques described herein areimplemented by one or more special-purpose computing devices. Thespecial-purpose computing devices may be hard-wired to perform thetechniques, or may include digital electronic devices such as one ormore application-specific integrated circuits (ASICs) or fieldprogrammable gate arrays (FPGAs) that are persistently programmed toperform the techniques, or may include one or more general purposehardware processors programmed to perform the techniques pursuant toprogram instructions in firmware, memory, other storage, or acombination. Such special-purpose computing devices may also combinecustom hard-wired logic, ASICs, or FPGAs with custom programming toaccomplish the techniques. The special-purpose computing devices may bedesktop computer systems, portable computer systems, handheld devices,networking devices or any other device that incorporates hard-wiredand/or program logic to implement the techniques.

For example, FIG. 10 is a block diagram that illustrates a computersystem 1000 upon which an embodiment of the invention may beimplemented. Computer system 1000 includes a bus 1002 or othercommunication mechanism for communicating information, and a hardwareprocessor 1004 coupled with bus 1002 for processing information.Hardware processor 1004 may be, for example, a general purposemicroprocessor.

Computer system 1000 also includes a main memory 1006, such as a randomaccess memory (RAM) or other dynamic storage device, coupled to bus 1002for storing information and instructions to be executed by processor1004. Main memory 1006 also may be used for storing temporary variablesor other intermediate information during execution of instructions to beexecuted by processor 1004. Such instructions, when stored innon-transitory storage media accessible to processor 1004, rendercomputer system 1000 into a special-purpose machine that is customizedto perform the operations specified in the instructions.

Computer system 1000 further includes a read only memory (ROM) 1008 orother static storage device coupled to bus 1002 for storing staticinformation and instructions for processor 1004. A storage device 1010,such as a magnetic disk or optical disk, is provided and coupled to bus1002 for storing information and instructions.

Computer system 1000 may be coupled via bus 1002 to a display 1012, suchas a cathode ray tube (CRT), for displaying information to a computeruser. An input device 1014, including alphanumeric and other keys, iscoupled to bus 1002 for communicating information and command selectionsto processor 1004. Another type of user input device is cursor control1016, such as a mouse, a trackball, or cursor direction keys forcommunicating direction information and command selections to processor1004 and for controlling cursor movement on display 1012. This inputdevice typically has two degrees of freedom in two axes, a first axis(e.g., x) and a second axis (e.g., y), that allows the device to specifypositions in a plane.

Computer system 1000 may implement the techniques described herein usingcustomized hard-wired logic, one or more ASICs or FPGAs, firmware and/orprogram logic which in combination with the computer system causes orprograms computer system 1000 to be a special-purpose machine. Accordingto one embodiment, the techniques herein are performed by computersystem 1000 in response to processor 1004 executing one or moresequences of one or more instructions contained in main memory 1006.Such instructions may be read into main memory 1006 from another storagemedium, such as storage device 1010. Execution of the sequences ofinstructions contained in main memory 1006 causes processor 1004 toperform the process steps described herein. In alternative embodiments,hard-wired circuitry may be used in place of or in combination withsoftware instructions.

The term “storage media” as used herein refers to any non-transitorymedia that store data and/or instructions that cause a machine tooperate in a specific fashion. Such storage media may comprisenon-volatile media and/or volatile media. Non-volatile media includes,for example, optical or magnetic disks, such as storage device 1010.Volatile media includes dynamic memory, such as main memory 1006. Commonforms of storage media include, for example, a floppy disk, a flexibledisk, hard disk, solid state drive, magnetic tape, or any other magneticdata storage medium, a CD-ROM, any other optical data storage medium,any physical medium with patterns of holes, a RAM, a PROM, and EPROM, aFLASH-EPROM, NVRAM, any other memory chip or cartridge,content-addressable memory (CAM), and ternary content-addressable memory(TCAM).

Storage media is distinct from but may be used in conjunction withtransmission media. Transmission media participates in transferringinformation between storage media. For example, transmission mediaincludes coaxial cables, copper wire and fiber optics, including thewires that comprise bus 1002. Transmission media can also take the formof acoustic or light waves, such as those generated during radio-waveand infra-red data communications.

Various forms of media may be involved in carrying one or more sequencesof one or more instructions to processor 1004 for execution. Forexample, the instructions may initially be carried on a magnetic disk orsolid state drive of a remote computer. The remote computer can load theinstructions into its dynamic memory and send the instructions over atelephone line using a modem. A modem local to computer system 1000 canreceive the data on the telephone line and use an infra-red transmitterto convert the data to an infra-red signal. An infra-red detector canreceive the data carried in the infra-red signal and appropriatecircuitry can place the data on bus 1002. Bus 1002 carries the data tomain memory 1006, from which processor 1004 retrieves and executes theinstructions. The instructions received by main memory 1006 mayoptionally be stored on storage device 1010 either before or afterexecution by processor 1004.

Computer system 1000 also includes a communication interface 1018coupled to bus 1002. Communication interface 1018 provides a two-waydata communication coupling to a network link 1020 that is connected toa local network 1022. For example, communication interface 1018 may bean integrated services digital network (ISDN) card, cable modem,satellite modem, or a modem to provide a data communication connectionto a corresponding type of telephone line. As another example,communication interface 1018 may be a local area network (LAN) card toprovide a data communication connection to a compatible LAN. Wirelesslinks may also be implemented. In any such implementation, communicationinterface 1018 sends and receives electrical, electromagnetic or opticalsignals that carry digital data streams representing various types ofinformation.

Network link 1020 typically provides data communication through one ormore networks to other data devices. For example, network link 1020 mayprovide a connection through local network 1022 to a host computer 1024or to data equipment operated by an Internet Service Provider (ISP)1026. ISP 1026 in turn provides data communication services through theworld wide packet data communication network now commonly referred to asthe “Internet” 1028. Local network 1022 and Internet 1028 both useelectrical, electromagnetic or optical signals that carry digital datastreams. The signals through the various networks and the signals onnetwork link 1020 and through communication interface 1018, which carrythe digital data to and from computer system 1000, are example forms oftransmission media.

Computer system 1000 can send messages and receive data, includingprogram code, through the network(s), network link 1020 andcommunication interface 1018. In the Internet example, a server 1030might transmit a requested code for an application program throughInternet 1028, ISP 1026, local network 1022 and communication interface1018.

The received code may be executed by processor 1004 as it is received,and/or stored in storage device 1010, or other non-volatile storage forlater execution.

What is claimed is:
 1. One or more non-transitory machine-readable mediastoring instructions which, when executed by one or more processors,cause: initiating a first process that traverses each of a plurality offrames on a call stack associated with a second process to perform aparticular operation with respect to each of the plurality of frames onthe call stack associated with the second process, wherein the pluralityof frames includes a first frame associated with a first function, and asecond frame associated with a second function that called the firstfunction; during a first time period in which the first frame has beentraversed to perform the particular operation, the second frame has notbeen traversed to perform the particular operation, and the secondprocess has not returned to the second function: executing, by thesecond process, a function epilogue of the first function, wherein thefunction epilogue comprises: determining whether a return condition istrue based on a first comparison between a first value of a stackpointer and a second value of a process-specific value associated withthe second process; responsive at least to determining that the returncondition is true: establishing a frame barrier for the second frame byjumping to a third function rather than returning to the secondfunction; during a second time period in which the first frame has beentraversed to perform the particular operation, the second frame has beentraversed to perform the particular operation, a third frame of theplurality of frames has not been traversed to perform the particularoperation, and the second process has not returned to the secondfunction: determining whether the return condition is true based on asecond comparison between a third value of the stack pointer and afourth value of the process-specific value associated with the secondprocess; responsive to determining that the return condition is nottrue: returning to the second function.
 2. The one or more media ofclaim 1, further storing instructions which, when executed by the one ormore processors, cause: during a third time period: determining whetherthe return condition is true based on a third comparison between a fifthvalue of the stack pointer and a sixth value of the process-specificvalue; responsive at least to determining that the return condition istrue: establishing a process-specific handshake associated with thesecond process.
 3. The one or more media of claim 1, further storinginstructions which, when executed by the one or more processors, cause:during a third time period: determining whether the return condition istrue based on a third comparison between a fifth value of the stackpointer and a sixth value of the process-specific value; responsive atleast to determining that the return condition is true: establishing asafepoint associated with a plurality of processes including the secondprocess.
 4. The one or more media of claim 1, further storinginstructions which, when executed by the one or more processors, cause:further responsive to determining that the return condition is true:identifying a particular purpose, of a plurality of purposes, forentering a slow path; responsive to determining that the particularpurpose is to establish the frame barrier: establishing the framebarrier.
 5. The one or more media of claim 1, wherein theprocess-specific value tracks a progress of a traversal of the pluralityof frames to perform the particular operation with respect to each ofthe plurality of frames.
 6. The one or more media of claim 1, whereinthe process-specific value is updated without suspending the secondprocess.
 7. The one or more media of claim 1, wherein at least one bitof the process-specific value serves as a flag to be used in determiningwhether a loop condition is true.
 8. The one or more media of claim 1,wherein the frame barrier is established without modifying any returnaddresses stored on the call stack.
 9. The one or more media of claim 1,wherein the first process is executed by a first thread and the secondprocess is executed by a second thread different than the first thread.10. The one or more media of claim 1, wherein the frame barrier for thesecond frame is removed after the second frame has been traversed toperform the particular operation, even though the third frame has notbeen traversed to perform the particular operation.
 11. The one or moremedia of claim 1, wherein the particular operation comprises at leastone of: marking objects referenced by a particular frame of theplurality of frames; relocating objects referenced by the particularframe of the plurality of frames; and remapping objects referenced bythe particular frame of the plurality of frames.
 12. The one or moremedia of claim 1, further storing instructions which, when executed bythe one or more processors, cause: executing the third functioncomprising: performing the particular operation with respect to thesecond frame; updating the process-specific value to indicate that theparticular operation has been performed with respect to the secondframe.
 13. The one or more media of claim 12, wherein performing theparticular operation with respect to the second frame comprises at leastone of: marking objects referenced by the second frame; relocatingobjects referenced by the second frame; and remapping objects referencedby the second frame.
 14. The one or more media of claim 1, furtherstoring instructions which, when executed by the one or more processors,cause: executing the third function comprising: waiting for the firstprocess to perform the particular operation with respect to the secondframe.
 15. The one or more media of claim 1, further storinginstructions which, when executed by the one or more processors, cause:executing the second function, even though at least one of the pluralityof frames has not been traversed to perform the particular operation.16. A system, comprising: at least one device including a hardwareprocessor; and the system being configured to perform operations,comprising: initiating a first process that traverses each of aplurality of frames on a call stack associated with a second process toperform a particular operation with respect to each of the plurality offrames on the call stack associated with the second process, wherein theplurality of frames includes a first frame associated with a firstfunction, and a second frame associated with a second function thatcalled the first function; during a first time period in which the firstframe has been traversed to perform the particular operation, the secondframe has not been traversed to perform the particular operation, andthe second process has not returned to the second function: executing,by the second process, a function epilogue of the first function,wherein the function epilogue comprises: determining whether a returncondition is true based on a first comparison between a first value of astack pointer and a second value of a process-specific value associatedwith the second process; responsive at least to determining that thereturn condition is true: establishing a frame barrier for the secondframe by jumping to a third function rather than returning to the secondfunction; during a second time period in which the first frame has beentraversed to perform the particular operation, the second frame has beentraversed to perform the particular operation, a third frame of theplurality of frames has not been traversed to perform the particularoperation, and the second process has not returned to the secondfunction: determining whether the return condition is true based on asecond comparison between a third value of the stack pointer and afourth value of the process-specific value associated with the secondprocess; responsive to determining that the return condition is nottrue: returning to the second function.
 17. A non-transitory computerreadable medium comprising instructions which, when executed by one ormore hardware processors, cause performance of operations comprising:initiating a first process that traverses each of a plurality of frameson a call stack associated with a second process, wherein the pluralityof frames includes a first frame associated with a first function, and asecond frame associated with a second function that called the firstfunction; during a first time period in which the first frame has beentraversed to perform the particular operation, the second frame has notbeen traversed to perform the particular operation, and the secondprocess has not returned to the second function: executing, by thesecond process, a loop within the first function, wherein executing theloop comprises: determining whether a loop condition is true based on aflag associated with a process-specific value; responsive at least todetermining that the loop condition is not true: returning to abeginning of the loop within the first function; executing, by thesecond process, a function epilogue of the first function, wherein thefunction epilogue comprises: determining whether a return condition istrue based on a first comparison between a stack pointer and theprocess-specific value; responsive at least to determining that thereturn condition is true: establishing a frame barrier for the secondframe by jumping to a third function rather than returning to the secondfunction.
 18. The one or more media of claim 17, wherein the flagassociated with the process-specific value comprises a least significantbit of the process-specific value.
 19. The one or more media of claim17, further storing instructions which, when executed by the one or moreprocessors, cause: executing, by the second process, a second loop,wherein executing the second loop comprises: determining whether theloop condition is true based on the flag associated with theprocess-specific value; responsive at least to determining that the loopcondition is true: jumping to a fourth function rather than returning toa second beginning of the second loop.
 20. The one or more media ofclaim 19, wherein the flag associated with the process-specific value isset such that the loop condition is true in order to establish at leastone of a process-specific handshake and a safepoint.